import ChessPieces from "../js/ChessPieces";

class ChessBoard{
    chessList=[]
    chessIdMap={};
    chessDraw=null;
    constructor(chessDraw){
        chessDraw.chessBoard=this;
        this.chessDraw=chessDraw;
        for(let i=0;i<10;i++){
            let row=[]
            for(let j=0;j<9;j++){
              row.push(new ChessPieces(this,{id:null,t:null,x:j,y:i}));
            }
            this.chessList.push(row);
        };
    }

    //初始化棋子
    initialize(data){
        let chessIdMap={}

        data.forEach(e => {
            chessIdMap[e.id]=e.t;
        });
        this.chessIdMap=chessIdMap;

        data.forEach(e => {
            this.setChessId(e);
        });
        this.chessList.forEach(t=>t.forEach(t2=>{
            this.setChess(t2,this.chessDraw.drawChess(t2));
        }));
        this.chessDraw.stage.update(); 
    }

    //初始化棋子
    initializeV2(data){
        this.chessIdMap=data.chessMap;
        this.chessList.forEach(t=>t.forEach(t2=>{
            this.chessDraw.stage.removeChild(t2.chess);
        }));
        let board=data.board;
        for (let y = 0; y < board.length; y++) {
            const row = board[y];
            for (let x = 0; x < row.length; x++) {
                this.setChessId({x,y,id:row[x]});
            }
        }
        this.chessList.forEach(t=>t.forEach(t2=>{
            this.setChess(t2,this.chessDraw.drawChess(t2));
        }));
        this.chessDraw.stage.update(); 
    }
    

    
    setNewChess(ele,terget){
        let xx=ele.x;
        let yy=ele.y;

        //设置新位置
        ele.x=terget.x;
        ele.y=terget.y;
        this.chessList[ele.y][ele.x]=ele;

        //更新之前位置
        let sourcesChess=new ChessPieces(this,{id:null,t:null,x:xx,y:yy});
        this.chessList[yy][xx]=sourcesChess;
        this.setChess(sourcesChess,this.chessDraw.drawChess(sourcesChess));
    }

    getChessName(chessId){
        return chessId==null?null:this.chessIdMap[chessId];
    }

    getChess(x,y){
        return this.chessList[y][x];
    }

    setChessId(ele){
        this.getChess(ele.x,ele.y).setChessId(ele.id);
    }

    setChess(ele,chess){
        this.getChess(ele.x,ele.y).setChess(chess);
        chess.tergetData=this.getChess(ele.x,ele.y);
    }

    /**
     * 判断是否存在棋子
     * @param {*} x 
     * @param {*} y 
     * @returns 
     */
    existChess(x,y){
        return this.getChess(x,y).id!=null;
    }

   
    /**
     * 判断横向或者纵向中间棋子数(不包含终点),不考虑没移动和同时移动x和y的情况
     * @param {*} ele 
     * @param {*} terget 
     * @returns 
     */
    countChess(ele,terget){
        let count=0;
        if(ele.x==terget.x){//x相同比纵向
            let min=Math.min(ele.y,terget.y)+1;
            let max=Math.max(ele.y,terget.y);
            for (let index = min; index < max; index++) {
                if(this.existChess(ele.x,index)){
                    count++;
                }
            }
        }
        else if(ele.y==terget.y){//x相同比纵向
            let min=Math.min(ele.x,terget.x)+1;
            let max=Math.max(ele.x,terget.x);
            for (let index = min; index < max; index++) {
                if(this.existChess(index,ele.y,)){
                    count++;
                }
            }
        }
        return count;
    } 
}

export default ChessBoard;